
local excel = load_excel('特殊奖池.xlsx','装备价值')
local list = {
    [1] = load_excel('装备设置.xlsx','1级'),
    [2] = load_excel('装备设置.xlsx','2级'),
    [3] = load_excel('装备设置.xlsx','3级'),
    [4] = load_excel('装备设置.xlsx','4级'),
    [5] = load_excel('装备设置.xlsx','5级'),
    [6] = load_excel('装备设置.xlsx','6级'),
}

local colors = {0xffffffff,0xff00ff00,0xff00ccff,0xffca42fd,0xffffff00,0xffff6800,0xffff0000,}
local util = require 'util.color'
function item_color(lv)
    local color = colors[lv] or colors[1]
    local color_text = util.text(color)
    return color,color_text
end

local tzjb = {1000,2000,4000,8000,16000,32000}
local items_list = {}
for lv,data in pairs(list) do
    local color,color_text = item_color(lv)
    local cost = excel[lv]
    local items = {}
    items_list[lv] = items
    for k,v in ipairs(data) do
        local name = v.name
        local texts = v.tip:split('唯一效果：')
        local title = color_text:format(('[%s阶]%s'):format(lv,name))
        v.tip1 = texts[1]
        if texts[2] then
            local tip = texts[2]:gsub('([%d]+[%d%.]?[%d]-)([\0%%]?)',function(num,ex)
                return ('|cff00ff00%s%s|r'):format(num,ex)
            end)
            v.tip2 = ('|n|n|cffeacb40唯一效果：|r%s'):format(tip)
        else
            v.tip2 = ''
        end
        v.name = name
        v.color = color
        v.used_flag = false
        v.item_level = lv
        v.item_class = '装备'
        v.item_type = 'campaign'
        v.title = title
        v.base_title = title
        v.state = text_unpack_attrs(texts[1])
        v.food = cost['杀敌']
        v.cooling_flag = true
        
        local mt = ac.item(name)
        local ex_tip1 = ''
        local ex_tip2 = '|cff9999993件同级装备可以在宠物合成|r'
        if v['可吞噬'] then
            v.tzjb = tzjb[lv]
            v.model = [[ktswp_bs.mdl]]
            v.once_flag = true
            v.exiocn = function(self)
                local lock = ac.player.self:get_data('不可吞噬')
                if lock and lock[self.name] then
                    self.show_effect = nil
                    return ''
                end
                self.show_effect = true
                return [[图标\ZB_KTS.blp]]
            end
            ex_tip1 = ('|cffff9900点击吞噬|r（|cffffff00%s|r金币|r）|n|n'):format(v.tzjb)
        end
        -- if lv<6 then
        --     ex_tip = '|cff999999两件同名装备自动合成|r'
        -- else
        --     ex_tip = '|cff9999996级以上(包括6级)装备在需要宠物合成|r'
        -- end
        v.update = function(self)
            local player = self.owner_player
            if player then
                local lock = player:get_data('不可吞噬') or {}
                if lock[self.name] and self.tz_set==nil then
                    self.tz_set = true
                    self.title = ('%s(|cffff0000已吞噬|r)'):format(self.base_title)
                    self:set_model([[Objects\InventoryItems\TreasureChest\treasurechest.mdx]])
                    self:set_color(self.color)
                end
            end
        end
        
        local tip = v.tip:gsub('([%d]+[%d%.]?[%d]-)([\0%%]?)',function(num,ex)
            return ('|cff00ff00%s%s|r'):format(num,ex)
        end)

        tip = tip:gsub('唯一效果：','|cffeacb40唯一效果：|r')
        v.ex_tip1 = ex_tip1
        v.ex_tip2 = ex_tip2

        v.tip = ('%s|cffeacb40装备属性：|r|n%s|n|n%s'):format(ex_tip1,tip,ex_tip2)
        v.base_tip = v.tip
        ac.lni('item',name,v)
        
        if v['无法合成']==nil then
            items[#items + 1] = v
        end
    end
end



function load_item_state(hero,item,add,itad)
    local state = item and item.state or item or {}
    for _,da in ipairs(state) do
        local key = da[1]
        local num = da[2]
        hero:add(key,num*add)
    end
end


function random_item_name(lv)
    local data = items_list[lv] or items_list[1]
    local temp = table.random(data)
    return temp.name
end


ac.game:event '单位-添加物品属性'(function(_,hero,item)
    load_item_state(hero,item,1)
    item:gc(hero:add_buff(item.name){
        cover_type = 1,
        cover_max = 1,
        keep = 1,
        item = item,
    })
end)

ac.game:event '单位-移除物品属性'(function(_,hero,item)
    load_item_state(hero,item,-1)
end)

ac.game:event '单位-即将获得物品'(function(_,hero,item)
    local player = hero.owner
    local list = player:get_data('不可吞噬') or {}
    local owner = item.owner_player
    if owner and player:get_data('开关-物品共享')==false then
        player:send_msg('不能拾取未开启|cff00ff00物品共享|r玩家的物品')
        return false,'物品被锁'
    end
    item.owner_player = player
    if hero:is_type('英雄') and item.item_class=='装备' and item.item_level>=6 then
        if list[item.name] then
            return false,'六级及以上的同名装备只能携带一个'
        end
        for it in hero:search_items() do
            if it.name == item.name and it~=item then
                return false,'六级及以上的同名装备只能携带一个'
            end
        end
    end

end)

--
local auto_list = {
    ['魔剑阿波菲斯'] = {'魔界之火','狱岩石','魔王之魂'},
}
local has_map = {}
for name,da in pairs(auto_list) do
    for _,key in ipairs(da) do
        has_map[key] = {name,da}
    end
end

local function cheak_upgrade(hero,name,needs)
    local player = hero.owner
    local items = {}
    for i,name in ipairs(needs) do
        local it = hero:find_item(name)
        if it==nil then
            return false
        end
        items[i] = it
    end
    for _,it in ipairs(items) do
        it:discard()
        it:remove()
    end
    local item = hero:add_item(name,{owner_player = player})
    player:send_msg(('|cff00ff00装备合成:%s'):format(item:get_title()))
    hero:add_effect([[Abilities\Spells\Items\AIem\AIemTarget.mdl]], 'origin'):remove()
end

ac.game:event '单位-获得物品'(function(_,hero,item)
    local info = has_map[item.name]
    if info then
        cheak_upgrade(hero,info[1],info[2])
    end
end)


--整理装备位置
function collate_item(hero,item)
    local player = hero.owner
    local point = player.item_point
	if type(item)=='string' then
		item = ac.item.create(item,point)
	end
    if item.name=='噬魂之心' then
        if player:get_data(item.name)==nil then
            player:set_data(item.name,true)
            local item = ac.item(item.name)
            if player:is_self() then
                item.model = nil
            end
        end
    end
    item.owner_player = player
    local lv = item.item_level or 0
    local a,b = int_grouping(lv,3)
    local p = point + {(b-2)*150,(-a+2)*150}
    local lock = player:get_data('不可吞噬') or {}
    if (item['可吞噬'] and lock[item.name]==nil and player:get_data(item.name)==nil) or lv==0 then
        item:set_point(player.item_saverect:random())
        return item
    end
    item:set_point(p)
    return item
end

--英雄获得装备
function hero_add_item(unit,item,haspet)
    local player = unit.owner
    local point = player.item_point
    local hero = player.hero
    local pet = player.pet
	if type(item)=='string' then
		item = ac.item.create(item,point)
	end
    item.owner_player = player
    if item.owner~=hero or item.owner~=pet then
        if hero and hero:is_alive() and #hero:get_items()<6 then
            item:give(hero)
            return item
        end
        if haspet and pet and pet:is_alive() and #pet:get_items()<6 then
            item:give(pet)
            return item
        end
        collate_item(hero,item)
    end


	return item
end


ac.wait(10,function()
    local test_data = {}
    for lv,data in ipairs(list) do
        for k,v in ipairs(data) do
            table.insert(test_data,v)
        end
    end
    ac.game:notify('测试-注册数据','物品测试',test_data,function(_,player,name)
        local hero = player.hero
        if hero then
            local it = hero:add_item(name) 
            it.owner_player = player
            hero_add_item(hero,it)
        end
    end)

end)